<template>
  <el-dialog
    v-model="visible"
    title="设备详情"
    width="600px"
    :before-close="handleClose"
  >
    <div v-loading="loading">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="设备ID">{{ deviceDetail.id }}</el-descriptions-item>
        <el-descriptions-item label="设备名称">{{ deviceDetail.name }}</el-descriptions-item>
        <el-descriptions-item label="设备型号">{{ deviceDetail.model }}</el-descriptions-item>
        <el-descriptions-item label="序列号">{{ deviceDetail.serial_number }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="getStatusType(deviceDetail.status)">
            {{ getStatusText(deviceDetail.status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="绑定用户">{{ deviceDetail.user_nickname || '未绑定' }}</el-descriptions-item>
        <el-descriptions-item label="固件版本">{{ deviceDetail.firmware_version }}</el-descriptions-item>
        <el-descriptions-item label="硬件版本">{{ deviceDetail.hardware_version }}</el-descriptions-item>
        <el-descriptions-item label="电量">
          <el-progress
            :percentage="deviceDetail.battery_level || 0"
            :color="getBatteryColor(deviceDetail.battery_level)"
            :stroke-width="8"
            text-inside
          />
        </el-descriptions-item>
        <el-descriptions-item label="信号强度">{{ deviceDetail.signal_strength }}%</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ formatDate(deviceDetail.created_at) }}</el-descriptions-item>
        <el-descriptions-item label="最后心跳">{{ formatDate(deviceDetail.last_heartbeat) }}</el-descriptions-item>
      </el-descriptions>
      
      <!-- 实时状态 -->
      <el-card style="margin-top: 20px;">
        <template #header>
          <span>实时状态</span>
        </template>
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="status-item">
              <div class="status-label">电流强度</div>
              <div class="status-value">{{ formatIntensity(deviceDetail.current_intensity) }}</div>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="status-item">
              <div class="status-label">频率</div>
              <div class="status-value">{{ formatFrequency(deviceDetail.current_frequency) }}</div>
            </div>
          </el-col>
        </el-row>
      </el-card>
    </div>
    
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
        <el-button type="primary" @click="viewDeviceDetail">查看详情</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getDeviceDetail } from '@/api/device'
import { formatDate, formatIntensity, formatFrequency } from '@/utils/format'

interface Device {
  id: string
  name: string
  model: string
  serial_number: string
  status: string
  user_nickname: string
  firmware_version: string
  hardware_version: string
  battery_level: number
  signal_strength: number
  current_intensity: number
  current_frequency: number
  created_at: string
  last_heartbeat: string
}

interface Props {
  modelValue: boolean
  device: any
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'refresh'): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const router = useRouter()
const visible = ref(false)
const loading = ref(false)

const deviceDetail = ref<Device>({
  id: '',
  name: '',
  model: '',
  serial_number: '',
  status: 'offline',
  user_nickname: '',
  firmware_version: '',
  hardware_version: '',
  battery_level: 0,
  signal_strength: 0,
  current_intensity: 0,
  current_frequency: 0,
  created_at: '',
  last_heartbeat: ''
})

// 监听modelValue变化
watch(() => props.modelValue, (val) => {
  visible.value = val
  if (val && props.device) {
    loadDeviceDetail(props.device.id)
  }
})

// 监听visible变化
watch(visible, (val) => {
  emit('update:modelValue', val)
  if (!val) {
    // 重置数据
    deviceDetail.value = {
      id: '',
      name: '',
      model: '',
      serial_number: '',
      status: 'offline',
      user_nickname: '',
      firmware_version: '',
      hardware_version: '',
      battery_level: 0,
      signal_strength: 0,
      current_intensity: 0,
      current_frequency: 0,
      created_at: '',
      last_heartbeat: ''
    }
  }
})

const loadDeviceDetail = async (deviceId: string) => {
  if (!deviceId) return
  
  loading.value = true
  try {
    const response = await getDeviceDetail(deviceId)
    deviceDetail.value = response.data
  } catch (error) {
    ElMessage.error('获取设备详情失败')
    console.error(error)
  } finally {
    loading.value = false
  }
}

const handleClose = () => {
  visible.value = false
}

const viewDeviceDetail = () => {
  handleClose()
  router.push(`/devices/detail/${props.device.id}`)
}

// 辅助方法
const getStatusType = (status: string): any => {
  const types: Record<string, string> = {
    online: 'success',
    offline: 'danger',
    maintenance: 'warning'
  }
  return types[status] || 'info'
}

const getStatusText = (status: string) => {
  const texts: Record<string, string> = {
    online: '在线',
    offline: '离线',
    maintenance: '维护中'
  }
  return texts[status] || status
}

const getBatteryColor = (level: number) => {
  if (level > 50) return '#67c23a'
  if (level > 20) return '#e6a23c'
  return '#f56c6c'
}
</script>

<style scoped lang="scss">
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.status-item {
  text-align: center;
  padding: 15px 0;
  
  .status-label {
    font-size: 14px;
    color: #606266;
    margin-bottom: 8px;
  }
  
  .status-value {
    font-size: 16px;
    font-weight: 500;
    color: #303133;
  }
}
</style>